package com.kikcn.demo.controllers;

import com.kikcn.demo.domain.Bookmark;
import com.kikcn.demo.domain.BookmarkFolder;
import com.kikcn.demo.services.BookmarkService;
import com.sun.istack.internal.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * User: wlai
 * Date: 4/10/15
 * Time: 10:52 AM
 */
@Controller
@RequestMapping("/bookmark")
public class BookmarkMgrController {
    private static final Integer PageSize = 10;
    private final static Logger logger = Logger.getLogger(BookmarkMgrController.class);
    @Autowired
    private BookmarkService service;

    @RequestMapping(params = "m=createFolder")
    public String createFolder() {
        return "bookmark/createFolder";
    }

    @RequestMapping(params = "m=saveFolder", method = RequestMethod.POST)
    public String saveFolder(@Valid BookmarkFolder folder, BindingResult result, ModelMap modelMap) {
        if (result.hasErrors()) {
            modelMap.put("errors", result.getAllErrors());
            return "error/showErrors";
        }
        this.service.saveFolder(folder);
        return "bookmark/done";
    }

    @RequestMapping(params = "m=listFolder")
    public String listFolder(HttpServletRequest request, Integer pageNumber) {
        if (pageNumber == null) pageNumber = 0;
        Pageable pageRequest = new PageRequest(pageNumber, PageSize);

        Page<BookmarkFolder> data = service.listFolder(pageRequest);
        request.setAttribute("page", data);
        return "bookmark/listFolder";
    }

    @RequestMapping(params = "m=createBookmark")
    public String createBookmark() {
        return "bookmark/createBookmark";
    }

    @RequestMapping(params = "m=saveBookmark", method = RequestMethod.POST)
    public String saveBookmark( @Valid @ModelAttribute("bookmark") Bookmark bookmark,
                               BindingResult result, ModelMap modelMap) {
        if (result.hasErrors()) {
            modelMap.put("errors", result.getAllErrors());
            return "error/showErrors";
        }
    //    if (folder != null &&folder.getId()!=null) bookmark.setParent(folder);
        this.service.saveBookmark(bookmark);
        return "bookmark/done";
    }

    @RequestMapping(params = "m=listBookmark")
    public String listBookmark(Integer folderId, ModelMap modelMap) {

        BookmarkFolder folder = null;
        if (folderId != null) {
            folder = service.findFolderById(folderId);
            modelMap.put("parent", folder);
        }

        List<BookmarkFolder> folders = service.findFolderByBookmarkFolder(folder);
        List<Bookmark> bookmarks = service.findByBookmarkFolder(folder);
        modelMap.put("folders", folders);
        modelMap.put("bookmarks", bookmarks);
        return "bookmark/listBookmark";
    }

    @ModelAttribute("folder")
    public BookmarkFolder loadBookmarkFolder(@RequestParam(value = "folderId", required = false) Integer folderId) {
        BookmarkFolder folder=null;
        if (folderId != null)
            folder= service.findFolderById(folderId);
        else folder= new BookmarkFolder();
        return folder;
    }

    @ModelAttribute("bookmark")
    public Bookmark loadBookmark(@RequestParam(value = "id", required = false) Integer bId) {
        if (bId != null) return service.findBookmarkById(bId);
        else return new Bookmark();
    }
}
